
import {
  _decorator, Component, Animation, KeyCode, RigidBody2D, Vec2, Canvas, Input, input,
  SystemEvent
} from 'cc';
const { ccclass, property } = _decorator;

@ccclass('Hero')
export class Hero extends Component {
  // [1]
  // dummy = '';
  // 移动步长
  step = 200;
  // 朝向：默认朝下
  direction = { x: -1, y: 0 };
  // 速度
  lv: Vec2;
  // 动画状态
  animationState: string;
  public keyInput = {};

  start() {
    // [3]
  }

  setInputActive(active: boolean) {
    if (active) {
      input.on(Input.EventType.KEY_DOWN, this.keydown, this)
      input.on(Input.EventType.KEY_UP, this.keyup, this)
    } else {
      input.off(Input.EventType.KEY_DOWN, this.keydown, this)
      input.off(Input.EventType.KEY_UP, this.keyup, this)
    }
  }
  keydown(e: any) {
    this.keyInput[e.keyCode] = true;
  }

  keyup(e) {
    this.keyInput[e.keyCode] = false;
  }
  playAnimation(animation: string) {
    this.node.getComponent(Animation).play(animation)
  }

  setAnimationState(state: string) {
    if (this.animationState === state) return;
    this.animationState = state;
    this.playAnimation(state);
  }

  setLinearVelocity() {
    this.lv = this.node.getComponent(RigidBody2D).linearVelocity;

    if (this.direction.x) {
      this.lv.x = this.step / 100
      this.lv.y = 0
    } else if (this.direction.y) {
      this.lv.x = 0
      this.lv.y = this.step / 100
    } else {
      this.lv.x = this.lv.y = 0
    }
    this.node.getComponent(RigidBody2D).linearVelocity = this.lv
  }

  setDirection() {
    if (this.keyInput[KeyCode.KEY_W] || this.keyInput[KeyCode.ARROW_UP]) {
      this.direction.y = 1
    }
    else if (this.keyInput[KeyCode.KEY_S] || this.keyInput[KeyCode.ARROW_DOWN]) {
      this.direction.y = -1
    } else {
      this.direction.y = 0
    }

    if (this.keyInput[KeyCode.KEY_A] || this.keyInput[KeyCode.ARROW_LEFT]) {
      this.direction.x = -1
    }
    else if (this.keyInput[KeyCode.KEY_D] || this.keyInput[KeyCode.ARROW_RIGHT]) {
      this.direction.x = 1
    } else {
      this.direction.x = 0
    }
  }

  getAnimationState() {
    let animationState = '';
    if (this.keyInput[KeyCode.KEY_W] || this.keyInput[KeyCode.ARROW_UP]) {
      animationState = 'hero_up'
    } else if (this.keyInput[KeyCode.KEY_S] || this.keyInput[KeyCode.ARROW_DOWN]) {
      animationState = 'hero_down'
    }

    if (this.keyInput[KeyCode.KEY_A] || this.keyInput[KeyCode.ARROW_LEFT]) {
      animationState = 'hero_left'
    } else if (this.keyInput[KeyCode.KEY_D] || this.keyInput[KeyCode.ARROW_RIGHT]) {
      animationState = 'hero_right'
    }
    return animationState;
  }

  setPosition(deltaTime: number) {
    let { x, y, z } = this.node.position;
    if (this.direction.x || this.direction.y) {
      x = x + this.lv.x * this.direction.x * deltaTime * this.step
      y = y + this.lv.y * this.direction.y * deltaTime * this.step
      let newx = x
      let newy = y
      if (x > 750) {
        newx = 750
      }
      if (x < -750) {
        newx = -750
      }
      if (y > 750) {
        newy = 750
      }
      if (y < -750) {
        newy = -750
      }
      this.node.setPosition(newx, newy, z);
    }
  }

  update(deltaTime: number) {
    // 设置朝向
    this.setDirection()
    // 设置速度
    this.setLinearVelocity()
    // 设置位置
    this.setPosition(deltaTime)
    // 设置角色动画
    const animationState = this.getAnimationState()
    if (animationState) {
      this.setAnimationState(animationState);
    }
  }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.0/manual/en/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.0/manual/en/scripting/ccclass.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.0/manual/en/scripting/life-cycle-callbacks.html
 */
